package wzh.crm.db;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection;

import java.text.ParseException;
import java.util.Date;

import wzh.crm.vo.CustomerVo;

/**
 * Created by wangzehao on 2016/6/15.
 */
public class CustomerDB extends WebServiceDB<CustomerVo> {
    @Override
    protected CustomerVo toBean(JSONObject json) throws Exception {
        CustomerVo model=new CustomerVo();
        model.setId(json.optInt("customerid"));
        model.set客户名称(json.optString("customername"));
        model.set简介(json.optString("profile"));
        model.set客户类型(json.optInt("customertype"));
        model.set当前状态(json.optInt("customerstatus"));
        model.set地区(json.optInt("regionid"));
        model.set上级客户(json.optInt("parentcustomerid"));
        model.set客户来源(json.optString("customersource"));
        model.set公司规模(json.optInt("size"));
        model.set电话(json.optString("telephone"));
        model.set邮箱(json.optString("email"));
        model.set网址(json.optString("website"));
        model.set地址(json.optString("address"));
        model.set邮编(json.optString("zipcode"));
        model.set创建人(json.optInt("staffid"));
        model.set创建该客户的时间(parseDate(json.optString("createdate")));
        model.set备注(json.optString("customerremarks"));
        return model;
    }

    @Override
    protected void toPost(CustomerVo model, Connection c) {
        c.data("customerid", Integer.toString(model.getId()));
        c.data("customername", model.get客户名称());
        c.data("profile", model.get简介());
        c.data("customertype", Integer.toString(model.get客户类型()));
        c.data("customerstatus", Integer.toString(model.get当前状态()));
        c.data("regionid", Integer.toString(model.get地区()));
        c.data("parentcustomerid", Integer.toString(model.get上级客户()));
        c.data("customersource", model.get客户来源());
        c.data("size", Integer.toString(model.get公司规模()));
        c.data("telephone", model.get电话());
        c.data("email", model.get邮箱());
        c.data("website", model.get网址());
        c.data("address", model.get地址());
        c.data("zipcode", model.get邮编());
        c.data("staffid", Integer.toString(model.get创建人()));
        c.data("createdate", dateToString(model.get创建该客户的时间()));
        c.data("customerremarks", model.get备注());
    }

    @Override
    protected String listQueryMethod() {
        return "common_customer_json";
    }

    @Override
    protected String idQueryMethod() {
        return "customer_query_json";
    }

    @Override
    protected String createMethod() {
        return "customer_create_json";
    }

    @Override
    protected String modifyMethod() {
        return "customer_modify_json";
    }
}
